import {
  HttpApi
} from '../../../../model/index.js'
const httpApi = new HttpApi

Page({

  /**
   * 页面的初始数据
   */
  data: {
    records: [],
    start: 1,
    size: 300,
    activeTabIndex: '0',
    loading: false,
    showNullImg: false,
    hiddenStatu: false,
    hiddenStatu1: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */

  onLoad: function(options) {
    var login = wx.getStorageSync('login')

    this.setData({
      dict: wx.getStorageSync("dict")
    })
    //this.getData(1, this.data.size)
  },

  getData(_start, _size) {
    this.loading = true
    var _this = this
    var activeTabIndex = _this.data.activeTabIndex;
    var status = activeTabIndex;
    httpApi.orderList(_start, _size, status).then(res => {
      console.log("status:", status)
      // console.log("orderList:",res.data)
      var arr1 = _this.data.records
      arr1 = [];
      var arr2 = res.data.records
      arr1 = arr1.concat(arr2)

      for (var i = 0; i < arr1.length; i++) {
        if (arr1[i].status == "0") {
          arr1[i].status = '待接单'
        } else
        if (arr1[i].status == "1") {
          arr1[i].status = '已接单'
        } else
        if (arr1[i].status == "2") {
          arr1[i].status = '已完成'
        }
      }
      _this.setData({
        start: _start,
        records: arr1,
        loading: false
      })
      if (_this.data.records.length == 0) {
        _this.setData({
          showNullImg: true,
        })
      }
      // _this.setNewRecords()
    })
  },
  setNewRecords() {
    var app = getApp()
    var subDict = app.globalData.dict["预约单状态"]
    console.log("subDict:", subDict)

    var newRecords = this.data.records
    for (var i = 0; i < newRecords.length; i++) {
      newRecords[i].status = subDict[newRecords[i].status]
    }
    console.log("new records:", newRecords)
    this.setData({
      records: newRecords
    })
  },

  dict(type, index) {
    var dict = wx.getStorageSync('dict')
    for (var i = 0; i < dict.length; i++) {
      var d = dict[i]
      console.log("d:", d)
      if (d.type == type) {
        return d.dictData[index].label
      }
    }
  },


  onReachBottom() {
    console.log("reachbottom")
    // 下拉触底，先判断是否有请求正在进行中
    // 以及检查当前请求页数是不是小于数据总页数，如符合条件，则发送请求
    if (!this.loading) {
      this.getData(this.data.start + this.data.size, this.data.size)
    }
  },

  onHide() {
    clearInterval(this.data.time)
  },

  onUnload() {
    clearInterval(this.data.time)
  },
  /* 退出 */
  loginOut() {
    wx.clearStorageSync()
    wx.reLaunch({
      url: '/pages/login/login'
    })
  },
  goYhxq: function(e){
    wx.navigateTo({
      url: './goYhxq/goYhxq'
    });
    return;
  },
  tabChange(event) {
    var _this = this
    let index = event.currentTarget.dataset.index;
    if (index === 0){
      _this.setData({
        hiddenStatu: false,
        hiddenStatu1: true,
      });
    } else if (index === 1){
      _this.setData({
        hiddenStatu: true,
        hiddenStatu1: false,
      });
    }
    _this.setData({
      activeTabIndex: index
    });
    _this.onLoad();
  },
})